﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using Sunboxsoft.Common;

namespace Sunboxsoft.ASMS.Client.Domain.Service
{
    public class QueryService
    {
        /// <summary>
        /// 精确获取客户列表
        /// </summary>
        /// <param name="filter"></param>
        /// <returns></returns>
        public ListResult<WebRef.VCustomer> QueryCustomerAccurately(string filter)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ListResultOfVCustomer result = service.QueryCustomerAccurately(filter);
                if (result.IsSuccess)
                {
                    return new ListResult<WebRef.VCustomer>(result.Data.Items);
                }
                return new ListResult<WebRef.VCustomer>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ListResult<WebRef.VCustomer>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 分页获取客户列表
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.VCustomer> QueryCustomerPagedList(string filter, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfVCustomer result = service.QueryCustomerPagedList(filter, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.VCustomer>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.VCustomer>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.VCustomer>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取预约单列表
        /// </summary>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="beginDate"></param>
        /// <param name="endDate"></param>
        /// <param name="salesManagerId">销售经理</param>
        /// <param name="status"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.VBookingBill> QueryBookingBillPagedList(Guid businessId, Guid stationId, DateTime? beginDate, DateTime? endDate, Guid? salesManagerId, int status, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfVBookingBill result = service.QueryBookingBillPagedList(businessId, stationId, beginDate, endDate, salesManagerId, status, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.VBookingBill>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.VBookingBill>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.VBookingBill>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取汽车列表
        /// </summary>
        /// <param name="customerId">客户的id</param>
        /// <returns></returns>
        public ListResult<WebRef.VAutomobile> QueryAutomobileList(Guid customerId)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ListResultOfVAutomobile result = service.QueryAutomobileList(customerId);
                if (result.IsSuccess)
                {
                    return new ListResult<WebRef.VAutomobile>(result.Data.Items);
                }
                return new ListResult<WebRef.VAutomobile>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ListResult<WebRef.VAutomobile>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取特定岗位的员工列表
        /// </summary>
        /// <param name="filter">过滤条件</param>
        /// <param name="businessId">商家Id</param>
        /// <param name="stationId">店铺Id，如果为null则获取所有店铺的销售经理</param>
        /// <param name="post">岗位编号,取自Constant.StaffPost.??</param>
        /// <returns></returns>
        public ListResult<WebRef.VStaffPosition> QueryStaffPositionList(string filter, Guid businessId, Guid? stationId, string post)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ListResultOfVStaffPosition result = service.QueryStaffPositionList(filter, businessId, stationId, post);
                if (result.IsSuccess)
                {
                    return new ListResult<WebRef.VStaffPosition>(result.Data.Items);
                }
                return new ListResult<WebRef.VStaffPosition>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ListResult<WebRef.VStaffPosition>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取员工列表
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="status"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.EStaff> QueryStaffPagedList(string filter, Guid businessId, Guid stationId, int status, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfEStaff result = service.QueryStaffPagedList(filter, businessId, stationId, status, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.EStaff>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.EStaff>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.EStaff>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取汽车服务分类列表
        /// </summary>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.EServiceCategory> QueryServiceCategoryPagedList(Guid? businessId, Guid? stationId, int status, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfEServiceCategory result = service.QueryServiceCategoryPagedList(businessId, stationId, status, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.EServiceCategory>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.EServiceCategory>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.EServiceCategory>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取汽车服务项目列表
        /// </summary>
        /// <param name="categoryId"></param>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.VServiceItem> QueryServiceItemPagedList(string filter, Guid? categoryId, Guid? businessId, Guid? stationId, int status, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfVServiceItem result = service.QueryServiceItemPagedList(filter, categoryId, businessId, stationId, status, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.VServiceItem>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.VServiceItem>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.VServiceItem>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        public ListResult<WebRef.VStation> QueryStationListByStaff(Guid staffId)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ListResultOfVStation result = service.QueryStationListByStaff(staffId);
                if (result.IsSuccess)
                {
                    return new ListResult<WebRef.VStation>(result.Data.Items);
                }
                return new ListResult<WebRef.VStation>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ListResult<WebRef.VStation>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取付费项目列表
        /// </summary>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="status"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.EPayItem> QueryPayItemPagedList(Guid? businessId, Guid? stationId, int status, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfEPayItem result = service.QueryPayItemPagedList(businessId, stationId, status, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.EPayItem>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.EPayItem>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.EPayItem>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取商品列表
        /// </summary>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="category"></param>
        /// <param name="brand"></param>
        /// <param name="status"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.VMaterial> QueryMaterialPagedList(string filter, Guid? businessId, Guid? stationId, string category, string brand, int status, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfVMaterial result = service.QueryMaterialPagedList(filter, businessId, stationId, category, brand, status, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.VMaterial>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.VMaterial>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.VMaterial>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取库存商品列表
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="warehouseId"></param>
        /// <param name="category"></param>
        /// <param name="brand"></param>
        /// <param name="status"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.VStockMaterial> QueryStockMaterialPagedList(string filter, Guid? businessId, Guid? stationId, Guid? warehouseId, string category, string brand, int status, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfVStockMaterial result = service.QueryStockMaterialPagedList(filter, businessId, stationId,warehouseId, category, brand, status, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.VStockMaterial>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.VStockMaterial>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.VStockMaterial>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取物料并带库存数据
        /// </summary>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="materialId"></param>
        /// <returns></returns>
        public ListResult<WebRef.VMaterialWithStock> QueryStockMaterialPagedList(Guid businessId, Guid stationId, Guid materialId)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ListResultOfVMaterialWithStock result = service.QueryMaterialWithStockList(businessId, stationId, materialId);
                if (result.IsSuccess)
                {
                    return new ListResult<WebRef.VMaterialWithStock>(result.Data.Items);
                }
                return new ListResult<WebRef.VMaterialWithStock>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ListResult<WebRef.VMaterialWithStock>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取仓库列表
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="status"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.VWarehouse> QueryWarehousePagedList(string filter, Guid? businessId, Guid? stationId, int status, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfVWarehouse result = service.QueryWarehousePagedList(filter, businessId, stationId, status, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.VWarehouse>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.VWarehouse>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.VWarehouse>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 
        /// 获取库存情况
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="queryType"></param>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="warehouseId"></param>
        /// <param name="materialId"></param>
        /// <param name="category"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.VStock> QueryStockPagedList(string filter, int queryType, Guid? businessId, Guid? stationId, Guid? warehouseId, Guid? materialId, string category, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfVStock result = service.QueryStockPagedList(filter, queryType, businessId, stationId, warehouseId, materialId, category, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.VStock>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.VStock>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.VStock>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        public PagedResult<WebRef.StockWarning> QueryStockWarningPagedList(Guid? businessId, Guid? stationId, Guid? warehouseId, Guid? materialId, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfStockWarning result = service.QueryStockWarningPagedList(businessId, stationId, warehouseId, materialId, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.StockWarning>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.StockWarning>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.StockWarning>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 
        /// 获取出入库记录
        /// </summary>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="warehouseId"></param>
        /// <param name="materialId"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="type"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.VStockChangedRecord> QueryStockChangedRecordPagedList(Guid? businessId, Guid? stationId, Guid? warehouseId, Guid? materialId,
            DateTime? beginTime, DateTime? endTime, int type, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfVStockChangedRecord result = service.QueryStockChangedRecordPagedList(businessId, stationId, warehouseId, materialId, beginTime, endTime, type, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.VStockChangedRecord>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.VStockChangedRecord>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.VStockChangedRecord>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取供应商列表
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="status"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.VSupplier> QuerySupplierPagedList(string filter, Guid? businessId, Guid? stationId, int status, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfVSupplier result = service.QuerySupplierPagedList(filter, businessId, stationId, status, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.VSupplier>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.VSupplier>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.VSupplier>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        #region packge
        /// <summary>
        /// 获取套餐列表
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="status"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.EPackage> QueryPackagePagedList(string filter, Guid? businessId, Guid? stationId, int status, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfEPackage result = service.QueryPackagePagedList(filter, businessId, stationId, status, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.EPackage>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.EPackage>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.EPackage>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取套餐的项目列表
        /// </summary>
        /// <param name="packageId"></param>
        /// <returns></returns>
        public ListResult<WebRef.EPackageItem> QueryPackageItemList(Guid packageId)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ListResultOfEPackageItem result = service.QueryPackageItemList(packageId);
                if (result.IsSuccess)
                {
                    return new ListResult<WebRef.EPackageItem>(result.Data.Items);
                }
                return new ListResult<WebRef.EPackageItem>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ListResult<WebRef.EPackageItem>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取价格列表
        /// </summary>
        /// <param name="targetId"></param>
        /// <param name="targetType">Constant.SalePriceTargetTypes.?</param>
        /// <returns></returns>
        public ListResult<WebRef.ESalesPrice> QuerySalePriceList(Guid targetId, int targetType)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ListResultOfESalesPrice result = service.QuerySalePriceList(targetId, targetType);
                if (result.IsSuccess)
                {
                    return new ListResult<WebRef.ESalesPrice>(result.Data.Items);
                }
                return new ListResult<WebRef.ESalesPrice>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ListResult<WebRef.ESalesPrice>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取套餐购买单列表
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="customerId"></param>
        /// <param name="salesManagerId"></param>
        /// <param name="status"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.VPackagePurchaseBill> QueryPackagePurchaseBill(string filter, Guid? businessId, Guid? stationId, Guid? customerId, Guid? salesManagerId, int status, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfVPackagePurchaseBill result = service.QueryPackagePurchaseBill(filter, businessId, stationId, customerId, salesManagerId, status, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.VPackagePurchaseBill>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.VPackagePurchaseBill>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.VPackagePurchaseBill>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取套餐变更记录
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.VPackageInstanceItemChangedRecord> QueryPackageInstanceItemChangedRecordPagedList(Guid? customerId, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfVPackageInstanceItemChangedRecord result = service.QueryPackageInstanceItemChangedRecordPagedList(customerId, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.VPackageInstanceItemChangedRecord>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.VPackageInstanceItemChangedRecord>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.VPackageInstanceItemChangedRecord>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取用户可用的套餐列表
        /// </summary>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="customerId"></param>
        /// <returns></returns>
        public ListResult<WebRef.PackageInstanceDetail> QueryEnabledPackageInstanceList(Guid businessId, Guid stationId, Guid customerId)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ListResultOfPackageInstanceDetail result = service.QueryEnabledPackageInstanceList(businessId, stationId, customerId);
                if (result.IsSuccess)
                {
                    return new ListResult<WebRef.PackageInstanceDetail>(result.Data.Items);
                }
                return new ListResult<WebRef.PackageInstanceDetail>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ListResult<WebRef.PackageInstanceDetail>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }
        #endregion

        /// <summary>
        /// 获取维修服务单列表
        /// </summary>
        /// <param name="filter"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="status"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.VMaintenanceBill> QueryMaintenanceBillPageList(string filter, DateTime? beginTime, DateTime? endTime, Guid? businessId, Guid? stationId,
            int[] statusArray,
            int[] serviceItemStatusArray,
            int[] materialStatusArray,
            int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfVMaintenanceBill result = service.QueryMaintenanceBillPageList(filter, beginTime, endTime, businessId, stationId,
                    statusArray,
                    serviceItemStatusArray,
                    materialStatusArray,
                    start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.VMaintenanceBill>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.VMaintenanceBill>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.VMaintenanceBill>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        #region business and station
        /// <summary>
        /// 获取商家列表
        /// </summary>
        /// <returns></returns>
        public ListResult<WebRef.EBusiness> QueryBusinessList()
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ListResultOfEBusiness result = service.QueryBusinessList();
                if (result.IsSuccess)
                {
                    return new ListResult<WebRef.EBusiness>(result.Data.Items);
                }
                return new ListResult<WebRef.EBusiness>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ListResult<WebRef.EBusiness>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取店家列表
        /// </summary>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <returns></returns>
        public ListResult<WebRef.VStation> QueryStationList(Guid? businessId, Guid? stationId)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ListResultOfVStation result = service.QueryStationList(businessId, stationId);
                if (result.IsSuccess)
                {
                    return new ListResult<WebRef.VStation>(result.Data.Items);
                }
                return new ListResult<WebRef.VStation>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ListResult<WebRef.VStation>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }
        #endregion

        #region recharge

        /// <summary>
        /// 获取充值项列表
        /// </summary>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="status"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.ERechargeItem> QueryRechargeItemPagedList(Guid? businessId, Guid? stationId, int status, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfERechargeItem result = service.QueryRechargeItemPagedList(businessId, stationId, status, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.ERechargeItem>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.ERechargeItem>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.ERechargeItem>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取充值单列表
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="status"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.VRechargeBill> QueryRechargeBillPagedList(Guid? customerId, Guid? businessId, Guid? stationId, int status, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfVRechargeBill result = service.QueryRechargeBillPagedList(customerId, businessId, stationId, status, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.VRechargeBill>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.VRechargeBill>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.VRechargeBill>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        #endregion

        #region capital

        /// <summary>
        /// 获取资金入账单列表
        /// </summary>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="changedType"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.VCapitalIncreaseBill> QueryCapitalIncreaseBillPagedList(Guid? businessId, Guid? stationId, DateTime? beginTime, DateTime? endTime, int changedType, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfVCapitalIncreaseBill result = service.QueryCapitalIncreaseBillPagedList(businessId, stationId, beginTime, endTime, changedType, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.VCapitalIncreaseBill>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.VCapitalIncreaseBill>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.VCapitalIncreaseBill>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取资金出账单列表
        /// </summary>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="changedType"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.VCapitalDecreaseBill> QueryCapitalDecreaseBillPagedList(Guid? businessId, Guid? stationId, DateTime? beginTime, DateTime? endTime, int changedType, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfVCapitalDecreaseBill result = service.QueryCapitalDecreaseBillPagedList(businessId, stationId, beginTime, endTime, changedType, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.VCapitalDecreaseBill>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.VCapitalDecreaseBill>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.VCapitalDecreaseBill>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }

        /// <summary>
        /// 获取资金流水明细
        /// </summary>
        /// <param name="businessId"></param>
        /// <param name="stationId"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <param name="changedType"></param>
        /// <param name="payChannel"></param>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public PagedResult<WebRef.VCapitalChangedRecord> QueryCapitalChangedRecordPagedList(Guid? businessId, Guid? stationId, DateTime? beginTime, DateTime? endTime, int changedType, string payChannel, int start, int limit)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.PagedResultOfVCapitalChangedRecord result = service.QueryCapitalChangedRecordPagedList(businessId, stationId, beginTime, endTime, changedType, payChannel, start, limit);
                if (result.IsSuccess)
                {
                    return new PagedResult<WebRef.VCapitalChangedRecord>(result.Data.Start, result.Data.Limit, result.Data.Total, result.Data.Items);
                }
                return new PagedResult<WebRef.VCapitalChangedRecord>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new PagedResult<WebRef.VCapitalChangedRecord>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }
        #endregion

        /// <summary>
        /// 获取单据头信息
        /// </summary>
        /// <param name="billNumber"></param>
        /// <returns></returns>
        public ListResult<WebRef.EBillHeader> QueryBillHeaderList(string billNumber)
        {
            WebRef.ASMSWebService service = RemoteInfo.GetASMSService();
            try
            {
                WebRef.ListResultOfEBillHeader result = service.QueryBillHeaderList(billNumber);
                if (result.IsSuccess)
                {
                    return new ListResult<WebRef.EBillHeader>(result.Data.Items);
                }
                return new ListResult<WebRef.EBillHeader>(result.ErrorCode, result.Message);
            }
            catch (Exception ex)
            {
                return new ListResult<WebRef.EBillHeader>(1, ex.Message);
            }
            finally
            {
                RemoteInfo.ReleaseASMSService(service);
            }
        }
    }
}
